#include <iostream>
#include <vector>
#include <memory>

// 命令接口
class Command {
public:
    virtual ~Command() {}
    virtual void execute() = 0;
};

// 接收者类
class Light {
public:
    void turnOn() {
        std::cout << "Light is on." << std::endl;
    }

    void turnOff() {
        std::cout << "Light is off." << std::endl;
    }
};

// 具体命令类：开灯
class LightOnCommand : public Command {
private:
    Light& light;

public:
    LightOnCommand(Light& light) : light(light) {}

    void execute() override {
        light.turnOn();
    }
};

// 具体命令类：关灯
class LightOffCommand : public Command {
private:
    Light& light;

public:
    LightOffCommand(Light& light) : light(light) {}

    void execute() override {
        light.turnOff();
    }
};

// 调用者类
class RemoteControl {
private:
    std::vector<std::unique_ptr<Command>> onCommands;
    std::vector<std::unique_ptr<Command>> offCommands;

public:
    void setCommand(size_t slot, std::unique_ptr<Command> onCmd, std::unique_ptr<Command> offCmd) {
        if (slot >= onCommands.size()) {
            onCommands.resize(slot + 1);
            offCommands.resize(slot + 1);
        }
        onCommands[slot] = std::move(onCmd);
        offCommands[slot] = std::move(offCmd);
    }

    void pressOnButton(size_t slot) {
        if (slot < onCommands.size() && onCommands[slot] != nullptr) {
            onCommands[slot]->execute();
        }
    }

    void pressOffButton(size_t slot) {
        if (slot < offCommands.size() && offCommands[slot] != nullptr) {
            offCommands[slot]->execute();
        }
    }
};

// 客户端代码
int main() {
    Light livingRoomLight;
    RemoteControl remote;

    remote.setCommand(0, std::make_unique<LightOnCommand>(livingRoomLight), std::make_unique<LightOffCommand>(livingRoomLight));

    remote.pressOnButton(0); // Light is on.
    remote.pressOffButton(0); // Light is off.

    return 0;
}